package mysql_9_18;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ExamResultDao extends ConnectDao {
	public boolean isGradeSubject(int gradeId, int subjectNo) {
		Connection con = getConnection();
		PreparedStatement st = null;
		String sql = "select subjectNo from subject where gradeID=?";
		ResultSet rs = null;
		try {
			st = con.prepareStatement(sql);
			st.setInt(1, gradeId);
			rs = st.executeQuery();
			while (rs.next()) {
				if (subjectNo == rs.getInt(1)) {
					return true;
				}
			}
			return false;
		} catch (SQLException e) {
			logger.debug(e.getMessage());
			return false;
		} finally {
			closeAll(con, st, rs);
		}
	}

	public int addExamResult(int stuNo, int subjectNo, float score) {
		Connection con = getConnection();
		PreparedStatement st = null;
		String sql = "insert into examResult(stuNo,subjectNo,score) values(?,?,?)";
		try {
			st = con.prepareStatement(sql);
			st.setInt(1, stuNo);
			st.setInt(2, subjectNo);
			st.setFloat(3, score);
			return st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			logger.debug(e.getMessage());
			return 0;
		} finally {
			closeAll(con, st, null);
		}
	}
}
